﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using BTN04.BUS;
using BTN04.DTO;

namespace BTN04.ADMIN
{
    public partial class thongkedoanhsotheothang : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ListItem item = new ListItem();
                item.Value = "0";
                item.Text = "--Chọn tháng--";
                ddThang.Items.Add(item);
                for (int i = 1; i <= 12; i++)
                {

                    item = new ListItem();
                    item.Value = i.ToString();
                    item.Text = i.ToString();
                    ddThang.Items.Add(item);
                }
                item = new ListItem();
                item.Value = "0";
                item.Text = "--Chọn năm--";
                ddNam.Items.Add(item);
                for (int i = DateTime.Now.Year - 20; i <= DateTime.Now.Year; i++)
                {
                    item = new ListItem();
                    item.Value = i.ToString();
                    item.Text = i.ToString();
                    ddNam.Items.Add(item);
                }
            }
            
            if (Request.QueryString["nam"] == null )
            {
                gvThongKe.Visible = false;
            }
            else
            {
                gvThongKe.Visible = true;
                LoadDS();
            }
        }

        

        public void LoadDS()
        {
            HoaDonBUS hdBUS = new HoaDonBUS();
            LoaiSanPhamBUS lspBUS = new LoaiSanPhamBUS();
            NhaSanXuatBUS nsxBUS = new NhaSanXuatBUS();
            DataTable dt = new DataTable();
            int thang;
            if (Request.QueryString["thang"] != null)
                thang = Convert.ToInt32(Request.QueryString["thang"]);
            else
                thang = 0;
            int nam = Convert.ToInt32(Request.QueryString["nam"]);
           
            dt = hdBUS.ThongKeDoanhSoTheoThang(thang,nam);
            DataTable dt1 = new DataTable();
            DataColumn c1 = dt1.Columns.Add("STT", Type.GetType("System.Int32"));
            DataColumn c2 = dt1.Columns.Add("TenSanPham", Type.GetType("System.String"));
            DataColumn c3 = dt1.Columns.Add("TenLoaiSanPham", Type.GetType("System.String"));
            DataColumn c4 = dt1.Columns.Add("TenNSX", Type.GetType("System.String"));
            DataColumn c5 = dt1.Columns.Add("SoLuong", Type.GetType("System.Int32"));
            DataColumn c6 = dt1.Columns.Add("GiaBan", Type.GetType("System.Double"));
            DataColumn c7 = dt1.Columns.Add("ThanhTien", Type.GetType("System.Double"));
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                int masp = Convert.ToInt32(dt.Rows[i]["MaSanPham"].ToString());
                SanPhamDTO spDTO = new SanPhamDTO();
                spDTO = SanPhamBUS.LaySanPhamTheoMa(masp);
                LoaiSanPhamDTO lspDTO = new LoaiSanPhamDTO();
                lspDTO = lspBUS.LayLoaiSanPhamTheoMa(spDTO .MaLoaiSanPham );
                NhaSanXuatDTO nsxDTO = new NhaSanXuatDTO();
                nsxDTO = nsxBUS.LayNhaSanXuatTheoMa(spDTO.MaNhaSanXuat);
                DataRow r = dt1.NewRow();
                r[0] = (i + 1).ToString();
                r[1] = spDTO.TenSanPham.ToString();
                r[2] = lspDTO.TenLoaiSanPham.ToString();
                r[3] = nsxDTO.TenNhaSanXuat.ToString();
                int slmua = Convert.ToInt32(dt.Rows[i]["SoLuongMua"].ToString());
                r[4] = slmua.ToString();
                r[5] = spDTO.GiaBan.ToString();
                float thanhtien = slmua * spDTO.GiaBan;
                r[6] = thanhtien;
                dt1.Rows.Add(r);
            }
            gvThongKe.DataSource = dt1;
            gvThongKe.DataBind();
        }

        protected void btnThongKe_Click(object sender, EventArgs e)
        {
            int index1 = ddThang.SelectedIndex;
            int index2 = ddNam.SelectedIndex;
            int thang;
            int nam;
            if (index2==0)
            {
                Response.Redirect("thongkedoanhsotheothang.aspx");
            }
            else
            {
                thang = Convert.ToInt32(ddThang.Items[index1].Value);
                nam = Convert.ToInt32(ddNam.Items[index2].Value);
               
                if (thang != 0 && nam != 0)
                {
                    Response.Redirect("thongkedoanhsotheothang.aspx?thang=" + thang.ToString() + "&nam=" + nam.ToString());
                }
                else
                {
                    if (thang == 0 && nam != 0)
                    {
                        Response.Redirect("thongkedoanhsotheothang.aspx?nam=" + nam.ToString());
                    }
                    else
                    {
                        Response.Redirect("thongkedoanhsotheothang.aspx");
                    }
                }
            }

        }

        protected void gvThongKe_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            HoaDonBUS hdBUS = new HoaDonBUS();
            LoaiSanPhamBUS lspBUS = new LoaiSanPhamBUS();
            NhaSanXuatBUS nsxBUS = new NhaSanXuatBUS();
            DataTable dt = new DataTable();
            int thang = Convert.ToInt32(Request.QueryString["thang"]);
            int nam = Convert.ToInt32(Request.QueryString["nam"]);
            dt = hdBUS.ThongKeDoanhSoTheoThang(thang, nam);
            DataTable dt1 = new DataTable();
            DataColumn c1 = dt1.Columns.Add("STT", Type.GetType("System.Int32"));
            DataColumn c2 = dt1.Columns.Add("TenSanPham", Type.GetType("System.String"));
            DataColumn c3 = dt1.Columns.Add("TenLoaiSanPham", Type.GetType("System.String"));
            DataColumn c4 = dt1.Columns.Add("TenNSX", Type.GetType("System.String"));
            DataColumn c5 = dt1.Columns.Add("SoLuong", Type.GetType("System.Int32"));
            DataColumn c6 = dt1.Columns.Add("GiaBan", Type.GetType("System.Double"));
            DataColumn c7 = dt1.Columns.Add("ThanhTien", Type.GetType("System.Double"));
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                int masp = Convert.ToInt32(dt.Rows[i]["MaSanPham"].ToString());
                SanPhamDTO spDTO = new SanPhamDTO();
                spDTO = SanPhamBUS.LaySanPhamTheoMa(masp);
                LoaiSanPhamDTO lspDTO = new LoaiSanPhamDTO();
                lspDTO = lspBUS.LayLoaiSanPhamTheoMa(spDTO .MaLoaiSanPham );
                NhaSanXuatDTO nsxDTO = new NhaSanXuatDTO();
                nsxDTO = nsxBUS.LayNhaSanXuatTheoMa(spDTO.MaNhaSanXuat);
                DataRow r = dt1.NewRow();
                r[0] = (i + 1).ToString();
                r[1] = spDTO.TenSanPham.ToString();
                r[2] = lspDTO.TenLoaiSanPham.ToString();
                r[3] = nsxDTO.TenNhaSanXuat.ToString();
                int slmua = Convert.ToInt32(dt.Rows[i]["SoLuongMua"].ToString());
                r[4] = slmua.ToString();
                r[5] = spDTO.GiaBan.ToString();
                float thanhtien = slmua * spDTO.GiaBan;
                r[6] = thanhtien;
                dt1.Rows.Add(r);
            }
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int stt = Convert.ToInt32(gvThongKe.DataKeys[e.Row.RowIndex].Value);
                e.Row.Cells[0].Text = stt.ToString();
                e.Row.Cells[1].Text = dt1.Rows[stt - 1]["TenSanPham"].ToString();
                e.Row.Cells[2].Text = dt1.Rows[stt - 1]["TenLoaiSanPham"].ToString();
                e.Row.Cells[3].Text = dt1.Rows[stt - 1]["TenNSX"].ToString();
                e.Row.Cells[4].Text = dt1.Rows[stt - 1]["SoLuong"].ToString();
                e.Row.Cells[5].Text = dt1.Rows[stt - 1]["GiaBan"].ToString()+" VNĐ";
                e.Row.Cells[6].Text = dt1.Rows[stt - 1]["ThanhTien"].ToString()+" VNĐ";
            }
        }

        protected void gvThongKe_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void gvThongKe_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvThongKe.PageIndex = e.NewPageIndex;
            LoadDS();
        }
    }
}
